import numSplit from '@/utils/numSplit'
const fmoneyModel = function(element, binding) {
  let value = element.value
  if (value) {
    const tmpValue = value.replace(/,/g, '')
    if (!isNaN(tmpValue * 1)) {
      const decimal = binding.value
      if (decimal) {
        value = numSplit(tmpValue, ',', 3, decimal)
      } else {
        value = numSplit(tmpValue, ',', 3)
      }
    }
  }
  element.value = value
}

const fmoney = {
  bind: function(el, binding, vnode) {
  },
  inserted: function(el, binding, vnode) {
    const element = el.querySelector('.el-input__inner') || el
    element.addEventListener('blur', () => {
      fmoneyModel(element, binding)
    })
  },
  update: function(el, binding, vnode) {
    if (!vnode.data.model) {
      return
    }
    const value = vnode.data.model.value
    if (value) {
      vnode.data.model.callback(vnode.data.model.value.replace(/,/g, ''))
    }
  },
  componentUpdated: function(el, binding, vnode) {
  },
  unbind: function(el, binding, vnode) {

  }
}

export default fmoney
